from fastapi import APIRouter
from core.config import response_body,mysql_engine,mongo_engine
from sqlalchemy import text
router = APIRouter(prefix='/test')

@router.get('/tables',summary='查询数据库中的所有表名')
async def get_tables():
    """
    查询数据库中的所有表名
    """
    with mysql_engine.connect() as conn:
        result = conn.execute(text("SELECT table_name FROM information_schema.tables WHERE table_schema = DATABASE();")).mappings().all()
    response_body['data'] = {'result':result}
    return response_body

@router.get('/test_mongo',summary='测试mongo连接')
async def get_test_mongo():
    """
    查询mongo数据库所有集合名
    """

    try:
        mongo_engine.admin.command('ping')
        response_body['msg'] = 'success'
        response_body['data'] = {'result':mongo_engine['tools-simon'].list_collection_names()}
    except Exception as e:
        response_body['code'] = 500
        response_body['msg'] = f'连接mongo失败: {e}'
    return response_body